type SpO2Measurement = {
    value: {
        spo2: number;
        heartRate: number;
    };
    unit: {
        spo2: string;
        heartRate: string;
    };
    rawData: string;
    timestamp: string;
};

class SpO2Parser {
    static readonly SERVICE_UUID: string = '6E400001-B5A3-F393-E0A9-E50E24DCCA9E';
    static readonly DATA_CHARACTERISTIC: string = '6E400003-B5A3-F393-E0A9-E50E24DCCA9E';

    static parseData(hexString: string): SpO2Measurement | void {
        // 基础校验：十六进制字符串格式
        if (!hexString || hexString.length % 2 !== 0) {
            throw new Error('无效的十六进制字符串（长度需为偶数）');
        }
        // 转换为字节数组（每两个字符为一个字节）
        const bytes = [];
        for (let i = 0; i < hexString.length; i += 2) {
            const byteHex = hexString.substr(i, 2);
            bytes.push(parseInt(byteHex, 16)); // 转为十进制数值
        }

        // 校验帧头（固定为 aa55）
        if (bytes[0] !== 0xaa || bytes[1] !== 0x55) {
            throw new Error('帧头校验失败（非传感器有效报文）');
        }
        if (bytes.length === 12) {
            const bloodOxygenByte = bytes[5];  // 示例位置（需调整）
            const heartRateByte = bytes[6];    // 示例位置（需调整）
            if (heartRateByte >= 30 && heartRateByte <= 200 && bloodOxygenByte >= 0 && bloodOxygenByte <= 100) {
                console.log(heartRateByte, bloodOxygenByte)
                return {
                    value: {
                        spo2: bloodOxygenByte,
                        heartRate: heartRateByte,
                    },
                    unit: {
                        spo2: '%',
                        heartRate: 'bpm'
                    },
                    rawData: hexString,
                    timestamp: new Date().toISOString()
                }
            }


        }
    }
}

export default SpO2Parser;
export type {SpO2Measurement};
